package com.casic.ops.utils;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ArrayUtil;
import com.casic.ops.consts.Const;
import com.casic.ops.consts.MessageConst;
import com.casic.ops.consts.system.SystemEnvAttr;
import com.orion.utils.Exceptions;
import com.orion.utils.Strings;
import com.orion.utils.io.Files1;

import java.io.File;
import java.util.regex.Matcher;


/**
 * @ClassName ops
 * @Description 继承 <hutool> 的断言 , 这里不使用 注解的 参数校验,灵活.
 * @Author mianbin
 * @Date 2022/6/17 14:07
 * @Version 1.0
 **/
public class OpsAssert extends Assert {

    @SafeVarargs
    public static <T> T in(T t, T... ts) {
        notNull(t);
        notEmpty(ts);
        isTrue(ArrayUtil.contains(ts, t), MessageConst.INVALID_PARAM);
        return t;
    }

    @SafeVarargs
    public static void allNotNull(Object... objects) {
        if (objects != null) {
            for (Object t : objects) {
                notNull(t, MessageConst.ABSENT_PARAM);
            }
        }
        Valid.allNotBlank();
    }

    @SafeVarargs
    public static void allNotBlank(String... ss) {
        if (ss != null) {
            for (String s : ss) {
                notBlank(s, MessageConst.ABSENT_PARAM);
            }
        }
    }

    /**
     * 检查是否超过文件上传阈值
     *
     * @param size size
     */
    public static void checkUploadSize(Long size) {
        Valid.notNull(size);
        String uploadThreshold = SystemEnvAttr.SFTP_UPLOAD_THRESHOLD.getValue();
        if (size / Const.BUFFER_KB_1 / Const.BUFFER_KB_1 > Long.parseLong(uploadThreshold)) {
            throw Exceptions.argument(Strings.format(MessageConst.UPLOAD_TOO_LARGE, uploadThreshold, Files1.getSize(size)));
        }
    }

    /**
     * 检查路径是否合法化 即不包含 ./ ../
     *
     * @param path path
     */
    public static void checkNormalize(String path) {
        Valid.notBlank(path);
        Valid.isTrue(OpsFile.isNormalize(path), MessageConst.PATH_NOT_NORMALIZE);
    }

    public static void isSafe(boolean s) {
        isTrue(s, MessageConst.UNSAFE_OPERATOR);
    }

    public static <T extends Comparable<T>> T lt(T value, T refer) {
        return lt(value, refer, "VALID_NOT_LT");
    }

    public static <T extends Comparable<T>> T lt(T value, T refer, String message, Object... values) {
        notNull(value);
        notNull(refer);
        if (value.compareTo(refer) >= 0) {
            throw Exceptions.invalidArgument(Strings.format(message, values));
        }
        return value;
    }

    public static <T extends Comparable<T>> T lte(T value, T refer) {
        return lte(value, refer, "VALID_NOT_LT_EQ");
    }

    public static <T extends Comparable<T>> T lte(T value, T refer, String message, Object... values) {
        notNull(value);
        notNull(refer);
        if (value.compareTo(refer) > 0) {
            throw Exceptions.invalidArgument(Strings.format(message, values));
        }
        return value;
    }

    public static <T extends Comparable<T>> T gt(T value, T refer) {
        return gt(value, refer, "VALID_NOT_GT");
    }

    public static <T extends Comparable<T>> T gt(T value, T refer, String message, Object... values) {
        notNull(value);
        notNull(refer);
        if (value.compareTo(refer) <= 0) {
            throw Exceptions.invalidArgument(Strings.format(message, values));
        }
        return value;
    }

    public static <T extends Comparable<T>> T gte(T value, T refer) {
        return gte(value, refer, "VALID_NOT_GT_EQ");
    }

    public static <T extends Comparable<T>> T gte(T value, T refer, String message, Object... values) {
        notNull(value);
        notNull(refer);
        if (value.compareTo(refer) < 0) {
            throw Exceptions.invalidArgument(Strings.format(message, values));
        }
        return value;
    }

}
